package com.bdqn.zhang.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bdqn.zhang.pojo.Book;
import com.bdqn.zhang.pojo.User;
import com.bdqn.zhang.service.BookService;
import com.bdqn.zhang.service.UserService;
import netscape.javascript.JSObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import util.LayData;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private BookService bookService;
    @Resource
    private UserService userService;

//    @RequestMapping("/login")
////    RequestParam 只写名字 代表参数是必传的，如果不传 会抛400
//    public String login(@RequestParam(required = false,value = "username") String username,
//                        @RequestParam(required = false,value = "password") String password){
//        return "/index";
//
//    }
//    RequestParam 只写名字 代表参数是必传的，如果不传 会抛400
    @RequestMapping("/login")
    public String login2(User user, Model model, HttpSession session){
        User user1=userService.login(user);
        if(user1!=null){
            session.setAttribute("user",user1);
            model.addAttribute("user",user1);
            return "index";
        }else {
            return "login";
        }



    }

    @RequestMapping("/list")
    @ResponseBody
    public LayData getAll(){
        List<Book> bookList= bookService.getbook();
       return LayData.data(bookList.size(),bookList);
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "user/add";

    }

    @RequestMapping("/addSave")
    public String addSave(Book book,Model model){
      int count=bookService.addbook(book);
      if(count>0){
          return "index";
      }else {
          model.addAttribute("msg","新增失败");
          return "user/add";
      }

    }
    @RequestMapping("/del")
    public String delUserById(@RequestParam("Id") Integer id,Model model){
        int count=bookService.delete(id);
        if(count>0){
            return "redirect:list";
        }
        model.addAttribute("msg","删除失败");
        return "redirect:list";
    }

    @RequestMapping("/loginout")
    public String loginout(HttpSession session){
            session.removeAttribute("user");
            return "login";
        }

    @RequestMapping("/toUpload")
    public String toUpload(){
        return "user/upload";

    }
//     文件上传
    @RequestMapping("/upload")
    @ResponseBody
    public LayData upload(MultipartFile file)throws Exception{
        FileUtil.writeBytes(file.getBytes(),"E:\\Y2\\"+System.currentTimeMillis()+file.getOriginalFilename());
        return LayData.dataUpload(System.currentTimeMillis()+file.getOriginalFilename());
    }
    }


